package com.office.automate.exception.framework;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class FrameworkException extends Exception {
	private static final long serialVersionUID = 6866935249599548014L;
	protected String detailsErrorMessage = null;

	public FrameworkException (ExceptionKeys messageKey, Throwable cause) {
		super(messageKey.getExceptionKey(), cause);
		setDetailsErrorMessage(getDetailExceptionMessage(messageKey));
	}

	private String getDetailExceptionMessage(ExceptionKeys messageKey) {
		String detailErrorMessage = "";
		Properties prop = new Properties();
		String nameOfClass = messageKey.getClass().getSimpleName();
		String errorPropertyFile =  nameOfClass.substring(0, nameOfClass.length() - 13) + "Errors.properties";
    	try {
    		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    		prop.load(classLoader.getResourceAsStream("exceptions/"+errorPropertyFile));
     		detailErrorMessage = prop.getProperty(messageKey.getExceptionKey());
	 	} catch (Exception ex) {
	 		detailErrorMessage = messageKey.getExceptionKey();
	    }
		return detailErrorMessage;
	}

	private void setDetailsErrorMessage(String aDetailsErrorMessage) {
		detailsErrorMessage = aDetailsErrorMessage;
	}
}